// pages/index/pic-total/pic-total.js
var navigate = require('../../utils/navigate.js')
const common = require('../../utils/login.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 20,
    page: 0,
    list: [],
    variable: {
      userBtn: true
    },
    searchVal: '',
    item:{
      page: 'Pic',
      currentCity: '本地',
      city_isClicked: false,
      country_isClicked: true,
      defaultVal: '全国',
      userLocation: true,
      iconSelectCity: '../../image/Triangle@2x.png',
      lineWidth: '',
      marginLeft: '',
      closeImg: 'https://gitee.com/Anjina/vote-pic/raw/7b66d388a031fdd545ce9a4ffc6a31e74de6710c/assets/Combined%20Shape@2x.png',
      isInput: false,
      searchVal: ''
    },
      getPos: false
  },
  onLoad: function (options) {
    this.getList()
    this.isUserInfo()
  },
  toDetails: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../pic-details/pic-details?id=' + id,
    })
  },
  tabCity: function () {
    wx.navigateTo({
      url: '../addressSelect/addressSelect?defaultVal='+this.data.item.defaultVal,
    })
  },
  toSearch: function () {
    wx.navigateTo({
      url: '../search/search?searchVal=' + this.data.searchVal,
    })
  },
  searchFor: function (e) {
    var that = this
    var value = e.detail.value
    // var item = this.data.item
    if (value != null || value != 'undefined') {
      that.setData({
        isInput: true
      })
    }
    if (value === '') {
      that.setData({
        isInput: false
      })
    }
    that.setData({
      searchVal: value
    })
  },
  clear: function () {
    var item = this.data.item
    item.isInput = false
    item.searchVal = ''
    this.setData({
      item: item
    })
  },
  click_country: function () {
    var page = this
    navigate.click_country(page)
  },
  click_city: function () {
    var page = this
    navigate.click_city(page)
  },
  // 判断是否有用户信息
  isUserInfo() {
    let userInfo = wx.getStorageSync('userInfo');
    let userBtn = 'variable.userBtn';
    if (userInfo) {
      this.setData({
        [userBtn]: false
      })
    }
  },
  // 登录
  bindGetUserInfo(e) {
    let id = e.currentTarget.dataset.id;
    console.log(e);
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      common.wxLogin().then(data => {
        console.log(data);
        let userBtn = 'variable.userBtn';
        that.setData({
          token: data.token,
          [userBtn]: false
        })
        wx.setStorageSync('userInfo', data.token); // 保存用户信息
        that.Voting(id);  // 登录之后调用投票
      });
    }
  },
  // 点击投票
  toupiao(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id;
    this.Voting(id);
  },
  // 投票请求封装
  Voting: function (id) {
    console.log(id);
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/add/' + id,
      data: '',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '投票成功!',
          icon: 'success',
          duration: 2000
        })
        that.getList();
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 已投票
  Voted() {
    console.log('已投票');
  },
  // 获取新闻列表
  getList() {
    console.log(1);
    let that = this;
    // let header = common.getHeader();
    wx.request({
      url: 'https://m.feawin.com/vote/list/query?activityId=' + 1 + '&contentType=image',
      method: 'GET',
      data: {
        size: that.data.size, page: that.data.page
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success(res) {
        console.log(res.data.list);
        that.setData({
          list: res.data.list
        })
      },
      fail() {
        that.setData({
          err: true
        })
      }
    })
  },
})